﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class TermTypeController : Controller
    {
        public ActionResult TermTypeList()
        {
            return View();
        }
        //查询
        public ActionResult List(BD_TermType TermType)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "DelState", Operator = "=", Value = "0" });
            Expression<Func<BD_TermType, bool>> exp = SearchExpression.FindByGroup<BD_TermType>(TermType, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.BD_TermTypeBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            count = BLLSession.Current.BD_TermTypeBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        //添加
        public ActionResult AddTermType(BD_TermType TT)
        {
            var list1 = DALSession.Current.BD_TermTypeDAL.GetListBy(d => d.TermType == TT.TermType && d.DelState == "0");
            if (list1.Count == 0)
            {
                TT.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                TT.EditPerName = OperateContext.sessUserInfo.Name;
                TT.EditPerID = OperateContext.sessUserInfo.Id;
                TT.DelState = "0";
                BLLSession.Current.BD_TermTypeBLL.Add(TT);
                return Json(new Message() { msg = "保存成功", state = "success" });
            }
            else
            {
                return Json(new Message() { msg = "数据已存在！", state = "warning" });
            }
        }
        //修改
        public ActionResult EditTermType(BD_TermType TT)
        {
            TT.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            TT.EditPerName = OperateContext.sessUserInfo.Name;
            TT.EditPerID = OperateContext.sessUserInfo.Id;
            BLLSession.Current.BD_TermTypeBLL.ModifyBy(TT, t => t.ID == TT.ID, "TermType", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //删除
        public ActionResult DelTermType(int Id)
        {
            //BLLSession.Current.BD_TermTypeBLL.DelBy(d => d.ID == Id);//直接删除
            BD_TermType BD_TermType1 = DALSession.Current.BD_TermTypeDAL.GetListBy(d => d.ID == Id).FirstOrDefault();
            BD_TermType1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BD_TermType1.EditPerName = OperateContext.sessUserInfo.Name;
            BD_TermType1.EditPerID = OperateContext.sessUserInfo.Id;
            BD_TermType1.DelState = "1";
            BLLSession.Current.BD_TermTypeBLL.ModifyBy(BD_TermType1, t => t.ID == BD_TermType1.ID, "TermType", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "删除成功", state = "success" });
        }
    }
}
